Public Partial Class FrmFacturaCompra
    Inherits System.Web.UI.Page
    Public Concepto As CapaDeNegocios.nConcepto = New CapaDeNegocios.nConcepto
    Public Factura As CapaDeNegocios.nFacturaCompra = New CapaDeNegocios.nFacturaCompra
    Public DetalleFac As CapaDeNegocios.nDetalleFactura = New CapaDeNegocios.nDetalleFactura
    Public Comprobacion As CapaDeNegocios.nComprobaciones = New CapaDeNegocios.nComprobaciones
    Public Imputacion As CapaDeNegocios.nImputacion = New CapaDeNegocios.nImputacion
    Public Proveedor As CapaDeNegocios.nProveedor = New CapaDeNegocios.nProveedor
    Public Remito As CapaDeNegocios.nRemito = New CapaDeNegocios.nRemito
    Public DetalleRemito As CapaDeNegocios.nDetalleRemitoCompra = New CapaDeNegocios.nDetalleRemitoCompra
    Dim DtsConcepto As DataSet
    Dim DtsRemito As DataSet
    Dim ImporteTotal As Double


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Login") = "Anonimo" Then
            Response.Redirect("/")
        End If
        If Not Page.IsPostBack Then
            Calendario.SelectedDate = Date.Today()
            TxtSucursal.Focus()
            LLenarComboConceptos()
            LlenarComboProveedor()
            LinkFacturas.OnClientClick = "javascript:window.open('FrmListadoFacturaCompra.aspx?ID=" + HFFactura.ClientID + "&idboton=" + BtnRefrescar.ClientID + "','popupdos','Width=730,Height=600,Scrollbars=yes,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO');"
            DtsConcepto = New DataSet()
            DtsConcepto.Tables.Add("TblConcepto")
            DtsConcepto.Tables(0).Columns.Add(New DataColumn("Remito"))
            DtsConcepto.Tables(0).Columns.Add(New DataColumn("CodigoConcepto"))
            DtsConcepto.Tables(0).Columns.Add(New DataColumn("Descripcion"))
            DtsConcepto.Tables(0).Columns.Add(New DataColumn("Cantidad"))
            DtsConcepto.Tables(0).Columns.Add(New DataColumn("PrecioU"))
            DtsConcepto.Tables(0).Columns.Add(New DataColumn("CodigoImputacion"))
            DtsConcepto.Tables(0).Columns.Add(New DataColumn("DescripcionImp"))
            Session("DtsCompras") = DtsConcepto
            DtsRemito = New DataSet()
            DtsRemito.Tables.Add("TblRemito")
            DtsRemito.Tables(0).Columns.Add(New DataColumn("Remito"))
            DtsRemito.Tables(0).Columns.Add(New DataColumn("CodigoConcepto"))
            DtsRemito.Tables(0).Columns.Add(New DataColumn("Descripcion"))
            DtsRemito.Tables(0).Columns.Add(New DataColumn("Cantidad"))
            DtsRemito.Tables(0).Columns.Add(New DataColumn("PrecioU"))
            DtsRemito.Tables(0).Columns.Add(New DataColumn("CodigoImputacion"))
            DtsRemito.Tables(0).Columns.Add(New DataColumn("DescripcionImp"))
            Session("DtsDetalleR") = DtsRemito
            Session("Importe") = ImporteTotal
            TxtPrecioU.Text = "0,00"
            LinkImputaciones.OnClientClick = "javascript:var popup = window.open('FrmImputacionesCompras.aspx?Importe=" + LblTotal.Text + "',(new Date().getTime()),'Width=500,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
            LinkImputaciones.Visible = False
            LinkGuardar.Visible = False


        End If
    End Sub
    Sub LLenarComboConceptos()
        CboConcepto.Items.Clear()
        CboConcepto.Items.Add(New ListItem("Seleccione un Concepto", -1))
        CboConcepto.DataSource = Concepto.ListarConcepto(0, "", "", 0, "Compra")
        CboConcepto.DataTextField = "Descripcion"
        CboConcepto.DataValueField = "CodigoConcepto"
        CboConcepto.DataBind()

    End Sub
    
    
    Sub LlenarComboProveedor()
        CboProveedor.Items.Clear()
        CboProveedor.Items.Add(New ListItem("Seleccione un Proveedor", -1))
        CboProveedor.DataSource = Proveedor.Listar(0, "", "", "", "", "", "", "")
        CboProveedor.DataTextField = "RazonSocial"
        CboProveedor.DataValueField = "CodigoProv"
        CboProveedor.DataBind()
    End Sub
    Sub LLenarComboRemitos()
        CboRemitos.Items.Clear()
        CboRemitos.Items.Add(New ListItem("Seleccione un Remito", -1))
        CboRemitos.DataSource = Remito.ListarRemitos(0, "", "", Int(CboProveedor.SelectedValue.ToString()), 0, "Pendiente", "", "")
        CboRemitos.DataTextField = "Numero"
        CboRemitos.DataValueField = "CodigoRemito"
        CboRemitos.DataBind()
    End Sub
    Sub LimpiarFormulario()
        TxtCantidad.Text = ""
        LblTotal.Text = ""
        TxtNumero.Text = ""
        TxtNumero.Text = "00000000"
        TxtPrecioU.Text = ""
        TxtSucursal.Text = ""
        TxtPrecioU.Text = "0,00"
        TxtSucursal.Text = "0000"
        LLenarComboConceptos()
        CboCondicion.SelectedIndex = -1

        HFProveedor.Value = ""
        CboProveedor.SelectedIndex = -1
        CboProveedor.Enabled = True
        CboConcepto.Enabled = True
        TxtPrecioU.Enabled = True
        TxtCantidad.Enabled = True
        LinkAgregar.Enabled = True
        LinkAgregaRemito.Enabled = True
        LinkImputaciones.Visible = False
        LblError.Visible = False
        TxtObservacion.Text = ""
        TxtSubtotalR.Text = "0,00"
        Session("DtsCompras") = DtsConcepto
        Session("DtsDetalleR") = DtsRemito

        ImporteTotal = Session("Importe")
        Session("dtsimputaciones") = ""
        
        GridConcepto.DataBind()
        GridRemito.DataBind()
        LLenarComboConceptos()
        LinkGuardar.Visible = False
    End Sub
    Sub BorrarDataset(ByVal dts As DataSet)
        If dts.Tables(0).Rows.Count() > 0 Then
            dts.Tables(0).Rows.Remove(dts.Tables(0).Rows(0))
            BorrarDataset(dts)
        End If
    End Sub
    Private Function ControlarCampos() As Integer
        Dim Cont As Integer = 0
        LblError.Text = ""
        LblError.Visible = False

        If Not IsNumeric(TxtSucursal.Text) And TxtSucursal.Text > 0 Then
            LblError.Text = LblError.Text + "- Error, la Sucursal debe ser Numero"
            Cont = Cont + 1
        End If
        If Comprobacion.FN_CantCaracteres(TxtSucursal.Text, 4) = False Then
            LblError.Text = LblError.Text + "- Error, Faltan Numeros en la Sucursal"
            Cont = Cont + 1
        End If
        If Not IsNumeric(TxtNumero.Text) And TxtNumero.Text > 0 Then
            LblError.Text = LblError.Text + "- Error, el numero de Factura es numerico"
            Cont = Cont + 1
        End If
        If Comprobacion.FN_CantCaracteres(TxtNumero.Text, 8) = False Then
            LblError.Text = LblError.Text + "- Error, Faltan numeros en la factura"
            Cont = Cont + 1
        End If
        If CboCondicion.SelectedValue.ToString() = -1 Then
            LblError.Text = LblError.Text + "- Error, la Condicion no es Correcta"
            Cont = Cont + 1
        End If
        If CboProveedor.SelectedValue.ToString() = -1 Then
            LblError.Text = LblError.Text + "- Error, no selecciono proveedor"
            Cont = Cont + 1
        End If

        If LblTotal.Text = "" Then
            LblError.Text = LblError.Text + "-Error, debe calcular el Total del Comprobante"
        End If
        Return Cont
    End Function

    Protected Sub LinkGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkGuardar.Click
        Dim VarFactura As Integer
        Dim grid As New DataTable
        Dim cont As Integer
        cont = ControlarCampos()
        Dim Iva As Double
        Dim i As Integer
        Dim j As Integer
        Dim Condicion As String
        Dim NumeroFactura As String

        LblError.Text = ""
        If Int(CboCondicion.SelectedValue.ToString()) = -1 Then
            LblError.Text = LblError.Text + "- Error,no selecciono Condicion de compra."
            cont = cont + 1
        End If
        If Int(CboCondicion.SelectedValue.ToString()) = 0 Then
            Condicion = 0
        Else
            Condicion = (CboCondicion.SelectedValue.ToString())
        End If
        NumeroFactura = (CboLetra.SelectedValue.ToString() & Trim(TxtSucursal.Text) & "-" & Trim(TxtNumero.Text))
        If Factura.Existe(NumeroFactura, Int(HFProveedor.Value)) Then
            cont = cont + 1
            LblError.Text = "Error,El numero de Factura ya se ingreso con el Proveedor."
        End If

        If cont = 0 Then
            If Int(Condicion) > 0 Then
                LinkImputaciones.Visible = False
                LinkGuardar.Visible = True
                
                DtsConcepto = Session("DtsCompras")
                For i = 0 To DtsConcepto.Tables(0).Rows.Count - 1
                    Iva = Concepto.DarIva(DtsConcepto.Tables(0).Rows(i)("CodigoConcepto").ToString())
                Next
                VarFactura = Factura.InsertarFactura(Int(HFProveedor.Value), _
                    NumeroFactura, "Pendiente", LblTotal.Text, Iva, Condicion, Trim(TxtObservacion.Text))
                If Int(VarFactura) > 0 Then
                    For i = 0 To CboRemitos.Items.Count - 1
                        If Not CboRemitos.Items(i).Enabled Then
                            Remito.ModificarRemito(CboRemitos.SelectedValue.ToString(), "Cancelado", VarFactura)
                        End If
                    Next
                    DtsConcepto = Session("DtsCompras")
                    DtsRemito = Session("DtsDetalleR")
                    If DtsRemito.Tables(0).Rows.Count > 0 Then
                        For i = 0 To DtsRemito.Tables(0).Rows.Count - 1
                            For j = 0 To DtsConcepto.Tables(0).Rows.Count - 1
                                If DtsConcepto.Tables(0).Rows(j)("CodigoConcepto").ToString() = _
                                DtsRemito.Tables(0).Rows(i)("CodigoConcepto").ToString() Then
                                    DtsConcepto.Tables(0).Rows(i)("PrecioU") = Int(DtsConcepto.Tables(0).Rows(i)("PrecioU").ToString()) + Int(DtsRemito.Tables(0).Rows(i)("PrecioU").ToString())
                                    DtsConcepto.Tables(0).Rows(i)("Cantidad") = Int(DtsConcepto.Tables(0).Rows(i)("cantidad").ToString()) + Int(DtsRemito.Tables(0).Rows(i)("cantidad").ToString())
                                End If
                            Next
                        Next
                    End If
                    For i = 0 To DtsConcepto.Tables(0).Rows.Count - 1
                        Iva = Concepto.DarIva(DtsConcepto.Tables(0).Rows(i)("CodigoConcepto").ToString())
                        DetalleFac.insertar((DtsConcepto.Tables(0).Rows(i)("Cantidad").ToString()), _
                            (DtsConcepto.Tables(0).Rows(i)("PrecioU").ToString()), _
                            (DtsConcepto.Tables(0).Rows(i)("PrecioU").ToString() * DtsConcepto.Tables(0).Rows(i)("Cantidad").ToString()), _
                            ((DtsConcepto.Tables(0).Rows(i)("PrecioU").ToString() * DtsConcepto.Tables(0).Rows(i)("Cantidad").ToString()) * (Iva / 100) + _
                             (DtsConcepto.Tables(0).Rows(i)("PrecioU").ToString() * DtsConcepto.Tables(0).Rows(i)("Cantidad").ToString())), _
                             VarFactura, DtsConcepto.Tables(0).Rows(i)("CodigoConcepto").ToString())
                    Next

                    Me.Response.Write("<script languaje='javascript'>window.alert('Alta Correcta')</script>")
                    LimpiarFormulario()
                End If
            Else
                Dim DtsImputaciones As DataSet = Session("DtsImputaciones")
                
                If Verificar_Imputaciones(DtsImputaciones) Then
                    DtsConcepto = Session("DtsCompras")
                    Condicion = 0
                    For i = 0 To DtsConcepto.Tables(0).Rows.Count - 1
                        Iva = Concepto.DarIva(DtsConcepto.Tables(0).Rows(i)("CodigoConcepto").ToString())
                    Next
                    VarFactura = Factura.InsertarFactura(Int(HFProveedor.Value), _
                                            NumeroFactura, "Cancelado", CDbl(LblTotal.Text), CDbl(Iva), Condicion, Trim(TxtObservacion.Text))
                    
                    If Int(VarFactura) > 0 Then
                        For i = 0 To CboRemitos.Items.Count - 1
                            If CboRemitos.Items(i).Enabled = False Then
                                Remito.ModificarRemito(CboRemitos.Items(i).Value.ToString(), "Cancelado", VarFactura)
                            End If
                        Next
                        DtsRemito = Session("DtsDetalleR")
                        If DtsRemito.Tables(0).Rows.Count > 0 Then
                            For i = 0 To DtsRemito.Tables(0).Rows.Count - 1
                                For j = 0 To DtsConcepto.Tables(0).Rows.Count - 1
                                    If DtsConcepto.Tables(0).Rows(j)("CodigoConcepto").ToString() = _
                                    DtsRemito.Tables(0).Rows(i)("CodigoConcepto").ToString() Then
                                        DtsConcepto.Tables(0).Rows(i)("PrecioU") = Int(DtsConcepto.Tables(0).Rows(i)("PrecioU").ToString()) + Int(DtsRemito.Tables(0).Rows(i)("PrecioU").ToString())
                                        DtsConcepto.Tables(0).Rows(i)("Cantidad") = Int(DtsConcepto.Tables(0).Rows(i)("cantidad").ToString()) + Int(DtsRemito.Tables(0).Rows(i)("cantidad").ToString())
                                    End If
                                Next
                            Next
                        End If
                        DtsConcepto = Session("DtsCompras")
                        For i = 0 To DtsConcepto.Tables(0).Rows.Count - 1
                            Iva = Concepto.DarIva(DtsConcepto.Tables(0).Rows(i)("CodigoConcepto").ToString())
                            DetalleFac.insertar((DtsConcepto.Tables(0).Rows(i)("Cantidad").ToString()), _
                            (DtsConcepto.Tables(0).Rows(i)("PrecioU").ToString()), _
                            (DtsConcepto.Tables(0).Rows(i)("PrecioU").ToString() * DtsConcepto.Tables(0).Rows(i)("Cantidad").ToString()), _
                            ((DtsConcepto.Tables(0).Rows(i)("PrecioU").ToString() * DtsConcepto.Tables(0).Rows(i)("Cantidad").ToString()) * (Iva / 100) + _
                             (DtsConcepto.Tables(0).Rows(i)("PrecioU").ToString() * DtsConcepto.Tables(0).Rows(i)("Cantidad").ToString())), _
                             VarFactura, DtsConcepto.Tables(0).Rows(i)("CodigoConcepto").ToString())
                        Next
                        Factura.GrabarImputaciones(DtsImputaciones, VarFactura, Calendario.Text, HFProveedor.Value) 
                        LimpiarFormulario()
                    Else
                        LblError.Text = "Error, No se pudo guardar la Factura"
                        LblError.Visible = True
                    End If
                Else
                    LblError.Text = "Error, las imputaciones no fueron cargadas y su Factura es Contado"
                    LblError.Visible = True
            End If
            End If
        Else
            LblError.Visible = True
        End If
    End Sub

    Protected Function Verificar_Imputaciones(ByVal DtsImputaciones As DataSet) As Boolean
        If Not (DtsImputaciones Is Nothing) Then
            Dim total As Double
            total = CDbl(LblTotal.Text)
            For Each row As DataRow In DtsImputaciones.Tables(0).Rows
                total = total - CDbl(row("Total").ToString())
            Next
            Return total = 0
        End If
        Return False
    End Function

    Protected Sub LinkAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkAgregar.Click
        Dim Cont As Integer
        Cont = 0
        LblError.Text = ""
        LblError.Visible = False
        CboProveedor.Enabled = False
        If CboConcepto.SelectedValue.ToString() = -1 Then
            LblError.Text = "Error, El concepto no es Correcto"
            Cont = Cont + 1
        End If
        If CboConcepto.SelectedValue.ToString() <> -1 Then
            CboConcepto.Items(CboConcepto.SelectedIndex).Enabled = False
        End If
        If Not IsNumeric(TxtPrecioU.Text) Then
            LblError.Text = "Error, El precio es Numerico"
            Cont = Cont + 1
        End If

        If Not IsNumeric(TxtCantidad.Text) Then
            LblError.Text = "Error,la Cantidad es Numerica"
            Cont = Cont + 1
        End If

        If Cont = 0 Then
            Dim Iva As Double
            Dim Subtotal As Double
            Dim dr As DataRow
            DtsConcepto = Session("DtsCompras")
            dr = DtsConcepto.Tables(0).NewRow()
            dr("Remito") = CboConcepto.SelectedValue.ToString()
            dr("CodigoConcepto") = CboConcepto.SelectedValue.ToString()
            dr("Descripcion") = CboConcepto.SelectedItem.Text
            dr("Cantidad") = TxtCantidad.Text
            dr("PrecioU") = TxtPrecioU.Text
            dr("CodigoImputacion") = Concepto.PasarImputacion(Int(CboConcepto.SelectedValue.ToString()))
            dr("DescripcionImp") = Imputacion.ListarImputacion(Concepto.PasarImputacion(dr(0).ToString()), "", "").Tables(0).Rows(0)("descripcionImp").ToString()
            DtsConcepto.Tables(0).Rows.Add(dr)
            Iva = Concepto.DarIva(Int(CboConcepto.SelectedValue.ToString()))
            Subtotal = ((Iva / 100) * Math.Round(CDbl(TxtCantidad.Text * TxtPrecioU.Text), 2)) + (Math.Round(CDbl(TxtCantidad.Text * TxtPrecioU.Text), 2))
            HfCuenta.Value = Math.Round(CDbl(Subtotal), 2)

            
            LblTotal.Text = LblTotal.Text + Math.Round(CDbl(HfCuenta.Value), 2)
            GridConcepto.DataSource = DtsConcepto
            GridConcepto.DataBind()
           
            LinkImputaciones.OnClientClick = "javascript:var popup = window.open('FrmImputacionesCompras.aspx?Importe=" + LblTotal.Text + "',(new Date().getTime()),'Width=500,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"

            LinkGuardar.Visible = True
        Else
            LblError.Visible = True
            Cont = 0
        End If

    End Sub

    Protected Sub CboProveedor_SelectedIndexChanged1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboProveedor.SelectedIndexChanged
        LLenarComboRemitos()
        HFProveedor.Value = CboProveedor.SelectedValue.ToString()
    End Sub
    Sub LlenarGrid(ByVal index As Integer)
        CboProveedor.Enabled = False
        LblTotal.Text = "0,00"
        If index <> -1 Then
            CboRemitos.Items(CboRemitos.SelectedIndex).Enabled = False
        End If
        Dim dr As DataRow
        Dim datos As DataSet = New DataSet()
        Dim i As Integer
        Dim Iva As Double
        datos = DetalleRemito.ListarTodo(0, "", 0, 0, Int(CboRemitos.SelectedValue.ToString()))
        For i = 0 To datos.Tables(0).Rows.Count - 1
            DtsConcepto = Session("DtsCompras")
            dr = DtsConcepto.Tables(0).NewRow()
            dr("Remito") = CboRemitos.SelectedValue.ToString()
            dr("CodigoConcepto") = datos.Tables(0).Rows(i)("CodigoConcepto")
            dr("Descripcion") = datos.Tables(0).Rows(i)("descripcion")
            dr("Cantidad") = datos.Tables(0).Rows(i)("cantidad")
            dr("PrecioU") = datos.Tables(0).Rows(i)("PrecioU")
            dr("CodigoImputacion") = datos.Tables(0).Rows(i)("CodigoImputacion")
            dr("DescripcionImp") = datos.Tables(0).Rows(i)("DescripcionImp")
            DtsConcepto.Tables(0).Rows.Add(dr)
            Iva = Concepto.DarIva(datos.Tables(0).Rows(i)("CodigoConcepto"))

        Next
        GridConcepto.DataSource = DtsConcepto
        GridConcepto.DataBind()
    End Sub

    Protected Sub LinkAgregaRemito_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkAgregaRemito.Click
        If Int(CboRemitos.SelectedValue.ToString()) <> -1 Then

            HFProveedor.Value = CboProveedor.SelectedValue.ToString()
            CboProveedor.Enabled = False
            LblTotal.Text = "0,00"
            If CboRemitos.SelectedIndex <> -1 Then
                CboRemitos.Items(CboRemitos.SelectedIndex).Enabled = False
            End If


            Dim dr As DataRow

            Dim datosrem As DataSet = New DataSet()
            Dim i As Integer
            Dim Iva As Double
            datosrem = DetalleRemito.ListarTodo(0, "", 0, 0, Int(CboRemitos.SelectedValue.ToString()))
            For i = 0 To datosrem.Tables(0).Rows.Count - 1
                DtsRemito = Session("DtsDetalleR")
                dr = DtsRemito.Tables(0).NewRow()
                dr("Remito") = CboRemitos.SelectedValue.ToString()
                dr("CodigoConcepto") = datosrem.Tables(0).Rows(i)("CodigoConcepto")
                dr("Descripcion") = datosrem.Tables(0).Rows(i)("descripcion")
                dr("Cantidad") = datosrem.Tables(0).Rows(i)("cantidad")
                dr("PrecioU") = datosrem.Tables(0).Rows(i)("PrecioU")
                dr("CodigoImputacion") = datosrem.Tables(0).Rows(i)("CodigoImputacion")
                dr("DescripcionImp") = datosrem.Tables(0).Rows(i)("DescripcionImp")
                DtsRemito.Tables(0).Rows.Add(dr)
                Iva = Concepto.DarIva(datosrem.Tables(0).Rows(i)("CodigoConcepto"))
            Next
            GridRemito.DataSource = DtsRemito
            GridRemito.DataBind()
        Else
            LblError.Text = "Error,debe seleccionar un Remito"
            LblError.Visible = True
        End If

    End Sub
  
    Protected Sub GridRemito_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridRemito.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim b As TextBox = e.Row.FindControl("TxtPrecioU")
            Dim c As Label = e.Row.FindControl("LblPrecioU")
            Dim d As RangeValidator = e.Row.FindControl("Datoerror")
            b.Visible = c.Text = "" Or c.Text = "0"
            c.Visible = Not b.Visible And Not d.Visible
            If b.Visible = True Then
                e.Row.FindControl("LnkCalcular").Visible = True
            End If
        End If
    End Sub
    Protected Sub GridConcepto_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridConcepto.RowCommand
        Dim i As Integer = CInt(e.CommandSource.parent.parent.RowIndex)
        Dim Iva As Integer
        Dim Importe As Double
        If e.CommandName = "Remover" Then
            If e.CommandSource.Parent.Parent.FindControl("HFCodigo").value.ToString() <> "" Then
                Iva = Concepto.DarIva(Int(e.CommandSource.Parent.Parent.FindControl("HFCodigo").value.ToString()))
                CboConcepto.SelectedValue = e.CommandSource.Parent.Parent.FindControl("HFCodigo").value.ToString()
                CboConcepto.Items(CboConcepto.Items.IndexOf(CboConcepto.Items.FindByValue(CInt(e.CommandSource.Parent.Parent.FindControl("HFCodigo").value.ToString())))).Enabled = True
                DtsConcepto = Session("DtsCompras")
                Importe = CDbl(DtsConcepto.Tables(0).Rows(i)("PrecioU").ToString()) * CDbl(DtsConcepto.Tables(0).Rows(i)("Cantidad").ToString())
                LblTotal.Text = (CDbl(LblTotal.Text) - (Importe + Importe * Iva / 100))
                DtsConcepto.Tables(0).Rows.Remove(DtsConcepto.Tables(0).Rows(i))

            End If
            TxtPrecioU.Text = "0,00"
            TxtCantidad.Text = "0"
            LinkImputaciones.OnClientClick = "javascript:var popup = window.open('FrmImputacionesCompras.aspx?Importe=" + LblTotal.Text + "',(new Date().getTime()),'Width=500,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
            CboConcepto.SelectedValue = -1
            GridConcepto.DataSource = DtsConcepto
            GridConcepto.DataBind()
        End If
    End Sub
    Protected Sub GridRemito_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridRemito.RowCommand
        Dim Total As Double
        Dim i As Integer
        Dim j As Integer = CInt(e.CommandSource.parent.parent.RowIndex)
        Dim Subtotal As Double
        Dim Iva As Double
        Dim Control As Boolean = False
        If e.CommandName = "Remover" Then
            If e.CommandSource.Parent.Parent.FindControl("HFRemito").value.ToString() <> "" Then
                CboRemitos.SelectedValue = e.CommandSource.Parent.Parent.FindControl("HFRemito").value.ToString()
                CboRemitos.Items(CboRemitos.Items.IndexOf(CboRemitos.Items.FindByValue(CInt(e.CommandSource.Parent.Parent.FindControl("HFRemito").value.ToString())))).Enabled = True
                DtsRemito = Session("DtsDetalleR")
                DtsRemito.Tables(0).Rows.Remove(DtsRemito.Tables(0).Rows(j))

            End If
            GridRemito.DataSource = DtsRemito
            GridRemito.DataBind()

        End If
        If Control = False Then
            If e.CommandName = "Calcular" Then
                For i = 1 To e.CommandSource.Parent.Parent.Parent.rows.count() - 2
                    Iva = Concepto.DarIva(e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("HFCodigo").value())
                    If e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("LblPrecioU").text > 0 Then
                        Subtotal = CDbl((e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("LblPrecioU").text) * CDbl(e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("LblCantidad").text))
                        Total = Total + (((Iva / 100) * (CDbl(Subtotal))) + Subtotal)
                    End If
                    If e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("TxtPrecioU").text > 0 Then
                        DtsRemito = Session("DtsDetalleR")
                        Subtotal = CDbl((e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("TxtPrecioU").text) * CDbl(e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("LblCantidad").text))
                        Total = Total + (((Iva / 100) * (CDbl(Subtotal))) + Subtotal)
                        e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("LblPrecioU").text = e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("TxtPrecioU").text
                        e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("LblPrecioU").visible = True
                        e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("TxtPrecioU").visible = False
                        DtsRemito.Tables(0).Rows(i - 1)("PrecioU") = e.CommandSource.Parent.Parent.Parent.rows(i).FindControl("LblPrecioU").text
                    End If

                Next
                If Total <> 0 Then
                    TxtSubtotalR.Text = 0
                    TxtSubtotalR.Text = TxtSubtotalR.Text + Math.Round(CDbl(Total), 2)
                    LblTotal.Text = Math.Round(CDbl(ImporteTotal), 2) + Math.Round(CDbl(TxtSubtotalR.Text), 2)
                    LinkImputaciones.OnClientClick = "javascript:var popup = window.open('FrmImputacionesCompras.aspx?Importe=" + LblTotal.Text + "',(new Date().getTime()),'Width=500,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
                    LinkGuardar.Visible = True
                End If

            End If

        End If
    End Sub

    Protected Sub LinkCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkCancelar.Click
        LimpiarFormulario()
    End Sub

    Protected Sub LinkNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkNuevo.Click
        LinkCancelar_Click(sender, e)
    End Sub

    Protected Sub CboCondicion_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCondicion.SelectedIndexChanged
        If CboCondicion.SelectedItem.Text = "Contado" And LblTotal.Text <> "" Then
            LinkImputaciones.Visible = True
        Else
            LinkImputaciones.Visible = False
        End If

    End Sub

   
End Class